package su.mvc.solo.actions.user;

import com.opensymphony.xwork2.ActionSupport;
import su.mvc.solo.model.User;
import su.mvc.solo.service.UserService;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import java.util.List;

/**
 * Created by Rustam Ismailov
 * Date: Nov 14, 2009 : 11:47:06 AM
 */
@Action(results = {@Result(name = ActionSupport.INPUT, location = "user-input.jsp"),
        @Result(name = "users", type = "redirect", params = {"location", "user!show.action", "parse", "false"})})
public class UserAction extends ActionSupport{

    private UserService userService;

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    private List<User> users;

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    private boolean edit = false;

    public boolean isEdit() {
        return edit;
    }

    public void setEdit(boolean edit) {
        this.edit = edit;
    }

    public String input() {
        user = new User();
        return ActionSupport.INPUT;
    }

    public String save() {
        if (edit) userService.updateEntity(user);
        else userService.createEntity(user);
        return "users";
    }

    public String edit() {
        user = userService.getUser(user.getId());
        edit = true;
        return ActionSupport.INPUT;
    }
    
    public String delete() {
        user = userService.getJpaTemplate().find(User.class, user.getId());
        userService.removeEntity(user);
        return "users";
    }

    public String show() {
        users = userService.findAllUsers();
        return ActionSupport.SUCCESS;
    }



}
